package com.yanq.liancar.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.ApplyCarAmdActivity;
import com.yanq.liancar.activity.CMainActivity;
import com.yanq.liancar.activity.FindMessageActivity;
import com.yanq.liancar.activity.LoginActivity;
import com.yanq.liancar.activity.LotteryRecordActivity;
import com.yanq.liancar.activity.MailAddressActivity;
import com.yanq.liancar.activity.MyCarActivity;
import com.yanq.liancar.activity.MyFollowActivity;
import com.yanq.liancar.activity.MyOBDActivity;
import com.yanq.liancar.activity.MyPointsActivity;
import com.yanq.liancar.activity.SelectActivity;
import com.yanq.liancar.activity.ServiceOrderActivity;
import com.yanq.liancar.activity.SigInRecordActivity;
import com.yanq.liancar.activity.SmallBMySetActivity;
import com.yanq.liancar.activity.SmallCBuyVIPActivity;
import com.yanq.liancar.activity.SmallCExchangeVoucherActivity;
import com.yanq.liancar.activity.SmallCFuelFillingActivity;
import com.yanq.liancar.activity.SmallCNewsActivity;
import com.yanq.liancar.activity.SmallCPersonalInformation;
import com.yanq.liancar.activity.SmallCRescueActivity;
import com.yanq.liancar.activity.SmallCVoucherActivity;
import com.yanq.liancar.base.BaseLazyFragment;
import com.yanq.liancar.bean.QueryUserBaseMessageBean;
import com.yanq.liancar.bean.SignInListBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseLazyFragment {
    @BindView(R.id.loginll)
    TextView login;
    @BindView(R.id.ask_for)
    TextView askFor;
    @BindView(R.id.tv_myCar)
    TextView myCar;
    @BindView(R.id.tv_myOBD)
    TextView myOBD;
    @BindView(R.id.user_name)
    TextView user_name;
    @BindView(R.id.user_data_llll)
    LinearLayout user_data_ll;
    @BindView(R.id.tool_bar)
    RelativeLayout top;
    @BindView(R.id.nextll)
    ImageView next;
    @BindView(R.id.rl_top)
    LinearLayout top_view;
    @BindView(R.id.message_iv)
    ImageView messageIv;
    @BindView(R.id.img_head)
    ImageView img_head;
    @BindView(R.id.setting_iv)
    ImageView settingIv;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_Voucher)
    TextView tv_Voucher;
    @BindView(R.id.service_voucher)
    TextView service_voucher;
    @BindView(R.id.Exchange_voucher)
    TextView Exchange_voucher;
    @BindView(R.id.tv_myfollow)
    TextView tv_myfollow;
    @BindView(R.id.tv_buyVip)
    TextView tv_buyVip;
    @BindView(R.id.tv_service_order)
    TextView tv_service_order;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_choujiang)
    TextView tv_choujiang;
    @BindView(R.id.tv_myfind)
    TextView tv_myfind;
    @BindView(R.id.tv_jiayou)
    TextView tv_jiayou;
    @BindView(R.id.shop_order)
    TextView shop_order;
    @BindView(R.id.my_jifen)
    TextView my_jifen;
    @BindView(R.id.my_qiandao)
    TextView my_qiandao;
    @BindView(R.id.img_vip)
    ImageView vipImg;
    private int BigC;
    private List<String> dateList = new ArrayList<>(); //连续签到日期

    public static MineFragment newInstance(String name, String sign) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putString("sign", sign);
        MineFragment mineFragment = new MineFragment();
        mineFragment.setArguments(bundle);
        return mineFragment;
    }
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getSignIn();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
            user_data_ll.setVisibility(View.VISIBLE);
            login.setVisibility(View.GONE);
            next.setVisibility(View.VISIBLE);
            initOkGo();
        } else {
            login.setVisibility(View.VISIBLE);
            user_data_ll.setVisibility(View.GONE);
            next.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
    }


    @OnClick({R.id.kefu, R.id.message_iv, R.id.setting_iv, R.id.loginll, R.id.nextll, R.id.tv_myOBD, R.id.tv_myCar, R.id.ask_for, R.id.tv_Voucher, R.id.service_voucher, R.id.Exchange_voucher
            , R.id.tv_myfollow, R.id.tv_service_order, R.id.tv_address, R.id.tv_buyVip, R.id.textView5, R.id.tv_jiayou, R.id.tv_choujiang, R.id.tv_myfind, R.id.shop_order, R.id.my_jifen, R.id.my_qiandao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.kefu:
                //                    H5Activity.start(mActivity, "https://chat.kuaishangkf.com/#/?script=T4zEN8t53kAXTSGNrdPHBNbD");
//                String title = "智能客服";
//                ConsultSource source = new ConsultSource("", "我的", "custom information string");
//                Unicorn.openServiceActivity(mActivity, title, source);
                Intent intent0 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "4008055011"));
                intent0.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent0);
                break;
            case R.id.message_iv:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Bundle bundle = new Bundle();
                    bundle.putString("where", "HomeFragment");
                    RxActivityTool.skipActivity(mActivity, SmallCNewsActivity.class, bundle);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
            case R.id.setting_iv:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("where", 1);
                    RxActivityTool.skipActivity(mActivity, SmallBMySetActivity.class, bundle);
                } else {
                    RxActivityTool.skipActivity(mActivity, SelectActivity.class);
                }
                break;
            case R.id.tv_myfind:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    FindMessageActivity.start(mActivity);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
            case R.id.loginll:
                RxActivityTool.skipActivityAndFinish(mActivity, LoginActivity.class);
                break;
            case R.id.nextll:
                Bundle bundle2 = new Bundle();
                bundle2.putString("where", "MineFragment");
                RxActivityTool.skipActivity(mActivity, SmallCPersonalInformation.class, bundle2);
                break;
            case R.id.tv_myOBD:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Intent intent1 = new Intent(getContext(), MyOBDActivity.class);
                    startActivity(intent1);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
            case R.id.tv_myCar:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Intent intent = new Intent(getContext(), MyCarActivity.class);
                    intent.putExtra("isC", false);
                    startActivity(intent);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
            case R.id.ask_for:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    if (BigC < 2) {
                        //审核页面
                        Intent intent = new Intent(getContext(), ApplyCarAmdActivity.class);
                        startActivity(intent);
                    } else {
                        //大C界面
                        RxActivityTool.skipActivity(mActivity, CMainActivity.class);
                    }
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
            case R.id.tv_Voucher:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("where", 1);
                    RxActivityTool.skipActivity(mActivity, SmallCVoucherActivity.class, bundle);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.service_voucher:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putInt("where", 2);
                    RxActivityTool.skipActivity(mActivity, SmallCVoucherActivity.class, bundle1);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.Exchange_voucher:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, SmallCExchangeVoucherActivity.class);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.tv_myfollow:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, MyFollowActivity.class);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.tv_service_order:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, ServiceOrderActivity.class);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.tv_address:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Bundle bundle = new Bundle();
                    bundle.putString("where", "MineFragment");
                    RxActivityTool.skipActivity(mActivity, MailAddressActivity.class, bundle);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
            case R.id.tv_buyVip:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, SmallCBuyVIPActivity.class);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.textView5:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, SmallCRescueActivity.class);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.tv_choujiang:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, LotteryRecordActivity.class);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.tv_jiayou:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, SmallCFuelFillingActivity.class);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;

            case R.id.shop_order:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {

                }else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
            case R.id.my_jifen:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    RxActivityTool.skipActivity(mActivity, MyPointsActivity.class);
                }else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }

                break;
            case R.id.my_qiandao:
                if (!TextUtils.isEmpty(UserBean.getcToken(mActivity))) {
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList("dateList", (ArrayList<String>) dateList);
                    RxActivityTool.skipActivity(mActivity, SigInRecordActivity.class, bundle);
                } else {
                    RxActivityTool.skipActivity(mActivity, LoginActivity.class);
                }
                break;
        }

    }

    private void initOkGo() {
        OkGo.<String>post(Config.s + "llc/queryUserBaseMessage")
                .tag(this)
                .params("token", UserBean.getcToken(mActivity))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        QueryUserBaseMessageBean bean = gson.fromJson(response.body(), QueryUserBaseMessageBean.class);
                        if (bean.isIsSuccess()) {
                            RequestOptions options = new RequestOptions();
                            options.error(R.mipmap.vip_touxiang);
                            Glide.with(mActivity).load(bean.getData().getHeadPage()).apply(options).into(img_head);
                            user_name.setText(bean.getData().getNickname());
                            tvSign.setText(bean.getData().getSign());
                            BigC = bean.getData().getApplyStatus();
                            if (bean.getData().getVipState() != 0) {
                                vipImg.setVisibility(View.VISIBLE);
                                if (bean.getData().getVipState() == 1) {
                                    vipImg.setImageResource(R.mipmap.v1);
                                } else {
                                    vipImg.setImageResource(R.mipmap.v2);
                                }
                            } else {
                                vipImg.setVisibility(View.GONE);
                            }

                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }

                    }
                });
    }

    private void getSignIn() {
        OkGo.<String>get(Config.s + "llc/querySignRecord")
                .tag(this)
                .params("token", UserBean.getcToken(mActivity))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        SignInListBean bean = gson.fromJson(response.body(), SignInListBean.class);
                        if (bean.isIsSuccess()) {
                            dateList.clear();
                            dateList.addAll(bean.getData());

                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }

                });
    }


}
